﻿using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using GuanQL.Core.Domain.Accounts;

namespace GuanQL.Core.Stores.ModelBuilders
{
    public class UserModelBuilder : ICustomModelBuilder
    {
        public void Build(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>()
               .ToTable("Users");

            modelBuilder.Entity<Role>()
                .ToTable("Role");

            modelBuilder.Entity<IdentityUserClaim<Guid>>(b =>
            {
                b.HasKey(uc => uc.Id);
                b.ToTable("UserClaim");
            });

            modelBuilder.Entity<IdentityRoleClaim<Guid>>(b =>
            {
                b.HasKey(rc => rc.Id);
                b.ToTable("RoleClaim");
            });

            modelBuilder.Entity<UserRole>(b =>
            {
                b.HasKey(ur => new { ur.UserId, ur.RoleId });
                b.HasOne(ur => ur.Role).WithMany(r => r.Users).HasForeignKey(r => r.RoleId);
                b.HasOne(ur => ur.User).WithMany(u => u.Roles).HasForeignKey(u => u.UserId);
                b.ToTable("UserRole");
            });

            modelBuilder.Entity<IdentityUserLogin<Guid>>(b =>
            {
                b.ToTable("UserLogin");
            });

            modelBuilder.Entity<IdentityUserToken<Guid>>(b =>
            {
                b.ToTable("UserToken");
            });

            modelBuilder.Entity<User>(u =>
            {
                u.HasOne(x => x.Avatar)
               .WithMany()
               .HasForeignKey(x => x.AvatarId)
               .OnDelete(DeleteBehavior.Restrict);
            });

            modelBuilder.Entity<Tenant>();

            //modelBuilder.Entity<Family>(f =>
            //{
            //    f.HasOne(r => r.User).WithMany().OnDelete(DeleteBehavior.Restrict);
            //    f.HasOne(r => r.Community).WithMany().OnDelete(DeleteBehavior.Restrict);
            //});

            //modelBuilder.Entity<UserIdentityApplication>(b =>
            //{
            //    b.HasOne(x => x.User).WithMany().HasForeignKey(x => x.UserId).OnDelete(DeleteBehavior.Restrict);
            //    b.HasOne(x => x.Room).WithMany().HasForeignKey(x => x.RoomId).OnDelete(DeleteBehavior.Restrict);
            //});

            //modelBuilder.Entity<Agent>(u =>
            //{
            //    u.HasOne(x => x.User).WithOne(x => x.Agent).OnDelete(DeleteBehavior.Restrict);
            //});

            //modelBuilder.Entity<AgentCommunityMap>(u =>
            //{
            //    u.HasOne(x => x.Agent).WithMany().HasForeignKey(c => c.AgentId).OnDelete(DeleteBehavior.Restrict);
            //    u.HasOne(x => x.Community).WithMany().HasForeignKey(c => c.CommunityId).OnDelete(DeleteBehavior.Restrict);
            //});

            modelBuilder.Entity<UserAmountHistory>(h =>
            {
                h.HasOne(x => x.User).WithMany().HasForeignKey(x => x.UserId).OnDelete(DeleteBehavior.Restrict);
            });
        }
    }
}
